package com.cgt.web.service.impl;

import com.cgt.web.dao.IBanjiDao;
import com.cgt.web.dao.impl.BanjiDaoImpl;
import com.cgt.web.pojo.Banji;
import com.cgt.web.service.IBanjiService;
import com.cgt.web.util.PageInfo;

import java.util.List;

public class BanjiServiceImpl implements IBanjiService {
    IBanjiDao BanjiDao = new BanjiDaoImpl();
    @Override
    public List<Banji> selectAll() {
        return BanjiDao.selectAll();
    }

    @Override
    public void deleteById(int id) {
        BanjiDao.deleteById(id);
    }

    @Override
    public void addInf(Banji banji) {
        BanjiDao.addInf(banji);
    }

    @Override
    public Banji toUpdate(int id) {
        return BanjiDao.toUpdate(id);
    }

    @Override
    public void update(Banji banji) {
        BanjiDao.update(banji);
    }

    @Override
    public PageInfo<Banji> selectByPage(int pageNo, int pageSize) {
        int offset = (pageNo-1) * pageSize;
        List<Banji> list = BanjiDao.selectByPage(offset,pageSize);
        int totalCount = BanjiDao.selectTotalCount();
        int totalPage = (int)Math.ceil((double)totalCount / pageSize);
        PageInfo<Banji> pageInfo = new PageInfo<>(list,pageNo,pageSize,totalPage);
        return pageInfo;
    }
//    public  static void  main(String[] args){
//        int totalCount = 12;
//        int pageSize = 5;
//        System.out.println(totalCount / pageSize);//2
//        System.out.println((double)totalCount / pageSize);//2.4
//        System.out.println(Math.ceil((double)totalCount / pageSize));//3.0
//
//        int totalPage = (int)Math.ceil((double)totalCount / pageSize);
//        System.out.println(totalPage);//3
//    }
}
